import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

#定义常量
rnn_unit=10       #hidden layer units
input_size=178
output_size=1
lr=0.0006     #学习率
#——————————————————导入数据——————————————————————
path='G:\大三下\人工智能与深度学习\预测组赛题\\'
#df=pd.read_csv(f)     #读入数据
#data=df.iloc[:,0:6].values  #取第3-10列

def read_data(path):
    train_data=pd.read_csv(path+'train.csv',dtype={'sale_date':object,'is_pro':object,},encoding='utf-8')
    train_data['goodsid']=train_data['goodsid'].astype(int)
    train_data['goodsid']=train_data['goodsid'].astype('object')

    test_data=pd.read_csv(path+'test.csv',dtype={'sale_date':object,'is_pro':object},encoding='utf-8')
    test_data['goodsid']=test_data['goodsid'].astype(int)
    test_data['goodsid']=test_data['goodsid'].astype('object')

    date_ch=pd.read_csv(path+'date_ch.csv',dtype={'dim_date_id':object,'year_code':object,'day_week_cn':object,'week_day_code':object,'is_weekend':object,'official_holiday_code':object},encoding='gbk')
    date_ch.rename(columns={'dim_date_id':'sale_date'}, inplace=True)

    goods_ch=pd.read_csv(path+'goods_ch.csv',dtype={'catg_l_id':object,'season_class':object},encoding='utf-8')
    goods_ch['goodsid']=goods_ch['goodsid'].astype(int)
    goods_ch['goodsid']=goods_ch['goodsid'].astype('object')
    del date_ch['official_holiday_name']
    del date_ch['festival_name']
    del goods_ch['season_class_name']
    del goods_ch['div_name']
    return train_data,test_data,date_ch,goods_ch

def merge_attr(train_data,test_data,date_ch,goods_ch):
    raw_train=pd.merge(train_data,date_ch,how='inner',on='sale_date')
    raw_train=pd.merge(raw_train,goods_ch,how='inner',on='goodsid')
    raw_test=pd.merge(test_data,date_ch,how='inner',on='sale_date')
    raw_test=pd.merge(raw_test,goods_ch,how='inner',on='goodsid')
    return raw_train,raw_test

def preprocessing(path):
    train_data,test_data,date_ch,goods_ch=read_data(path)
    raw_train,raw_test=merge_attr(train_data,test_data,date_ch,goods_ch)
    n_train=raw_train.shape[0]
    train_cols=list(range(3,6))+[7]+list(range(11,14))+[15]+[17]+[20]
    test_cols=list(range(2,5))+[6]+list(range(10,13))+[14]+[16]+[19]
    all_features=pd.concat((raw_train.iloc[:,train_cols], raw_test.iloc[:,test_cols]))
    labels=raw_train.iloc[:,[2]][:n_train]
    info=raw_test.iloc[:,[0,1]]

    numeric_features=all_features.dtypes[all_features.dtypes!='object'].index
    #all_features[numeric_features]=all_features[numeric_features].apply(lambda x:(x-x.mean())/(x.std()))
    all_features[numeric_features]=all_features[numeric_features].fillna(0)
    
    attr_list=['year_code', 'day_week_cn','is_weekend','festival_code', 'season_class','catg_l_id','is_pro','week_day_code']
    all_features=pd.get_dummies(all_features,dummy_na=True,prefix=attr_list,columns=attr_list,dtype=int)
    
    train_features=all_features[:n_train]
    test_features=all_features[n_train:]
    train_features=pd.concat([train_features,labels],axis=1)
    
    return train_features,test_features,info
 #得到训练集和测试集数据
train_features,test_features,info=preprocessing(path)
train_data=train_features.iloc[:,0:].values
test_data=test_features.iloc[:,0:].values

#获取训练集
def get_train_data(batch_size=60,time_step=30,train_begin=0,train_end=1400):
    batch_index=[]
    data_train=train_data[train_begin:train_end]
    #normalized_train_data=(data_train-np.mean(data_train,axis=0))/np.std(data_train,axis=0)  #标准化
    normalized_train_data=data_train #预处理已经标准化
    train_x,train_y=[],[]   #训练集 
    for i in range(len(normalized_train_data)-time_step):
       if i % batch_size==0:
           batch_index.append(i)
       x=normalized_train_data[i:i+time_step,:178]
       y=normalized_train_data[i:i+time_step,178,np.newaxis]
       train_x.append(x.tolist())
       train_y.append(y.tolist())
    batch_index.append((len(normalized_train_data)-time_step))
   # print(',,,,',len(normalized_train_data),time_step)
    return batch_index,train_x,train_y



#获取测试集
def get_test_data(time_step=20,test_begin=0):
    data_test=test_data[test_begin:]
    mean=np.mean(data_test,axis=6)
    std=np.std(data_test,axis=6)
    normalized_test_data=(data_test-mean)/std  #标准化
    #normalized_test_data = data_test #预处理已经标准化
    size=(len(normalized_test_data)+time_step-1)//time_step  #有size个sample 
    test_x,test_y=[],[]  
    for i in range(size-1):
       x=normalized_test_data[i*time_step:(i+1)*time_step,:178]
       y=normalized_test_data[i*time_step:(i+1)*time_step,178]
       test_x.append(x.tolist())
       test_y.extend(y)
    test_x.append((normalized_test_data[(i+1)*time_step:,:178]).tolist())
    test_y.extend((normalized_test_data[(i+1)*time_step:,178]).tolist())
    return mean,std,test_x,test_y



#——————————————————定义神经网络变量——————————————————
#输入层、输出层权重、偏置

weights={
         'in':tf.Variable(tf.random.normal([input_size,rnn_unit])),
         'out':tf.Variable(tf.random.normal([rnn_unit,1]))
        }
biases={
        'in':tf.Variable(tf.constant(0.1,shape=[rnn_unit,])),
        'out':tf.Variable(tf.constant(0.1,shape=[1,]))
       }

#——————————————————定义神经网络变量——————————————————
def lstm(X):     
    batch_size=tf.shape(X)[0]
    time_step=tf.shape(X)[1]
    w_in=weights['in']
    b_in=biases['in']  
    input=tf.reshape(X,[-1,input_size])  #需要将tensor转成2维进行计算，计算后的结果作为隐藏层的输入
    input_rnn=tf.matmul(input,w_in)+b_in
    input_rnn=tf.reshape(input_rnn,[-1,time_step,rnn_unit])  #将tensor转成3维，作为lstm cell的输入
    cell=tf.nn.rnn_cell.BasicLSTMCell(rnn_unit)
    init_state=cell.zero_state(batch_size,dtype=tf.float32)
    output_rnn,final_states=tf.nn.dynamic_rnn(cell, input_rnn,initial_state=init_state, dtype=tf.float32)  #output_rnn是记录lstm每个输出节点的结果，final_states是最后一个cell的结果
    output=tf.reshape(output_rnn,[-1,rnn_unit]) #作为输出层的输入
    w_out=weights['out']
    b_out=biases['out']
    pred=tf.matmul(output,w_out)+b_out
    return pred,final_states



#——————————————————训练模型——————————————————
def train_lstm(batch_size=60,time_step=30,train_begin=0,train_end=1400):
    X=tf.placeholder(tf.float32, shape=[None,time_step,input_size])
    Y=tf.placeholder(tf.float32, shape=[None,time_step,output_size])
    batch_index,train_x,train_y=get_train_data(batch_size,time_step,train_begin,train_end)
    pred,_=lstm(X)
    #损失函数
    
    loss=tf.truediv(tf.reduce_sum(tf.abs(tf.reshape(pred,[-1])-tf.reshape(Y, [-1]))),tf.reduce_sum(tf.reshape(Y, [-1])))
    
    train_op=tf.train.AdamOptimizer(lr).minimize(loss)
    saver=tf.train.Saver(tf.global_variables(),max_to_keep=15)
    #module_file = tf.train.latest_checkpoint()    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        #saver.restore(sess, module_file)
        #重复训练10000次
        for i in range(200):
            for step in range(len(batch_index)-1):
                _,loss_=sess.run([train_op,loss],feed_dict={X:train_x[batch_index[step]:batch_index[step+1]],Y:train_y[batch_index[step]:batch_index[step+1]]})
            print(i,loss_)
            if i % 200==0:
                print("保存模型：",saver.save(sess,'预测.model',global_step=i))


train_lstm()


#————————————————预测模型————————————————————
def prediction(time_step=20):
    X=tf.placeholder(tf.float32, shape=[None,time_step,input_size])
    #Y=tf.placeholder(tf.float32, shape=[None,time_step,output_size])
    mean,std,test_x,test_y=get_test_data(time_step)
    pred,_=lstm(X)     
    saver=tf.train.Saver(tf.global_variables())
    with tf.Session() as sess:
        #参数恢复
        module_file = tf.train.latest_checkpoint()
        saver.restore(sess, module_file) 
        test_predict=[]
        for step in range(len(test_x)-1):
          prob=sess.run(pred,feed_dict={X:[test_x[step]]})   
          predict=prob.reshape((-1))
          test_predict.extend(predict)
        test_y=np.array(test_y)*std[178]+mean[178]
        test_predict=np.array(test_predict)*std[178]+mean[178]
        acc=np.average(np.abs(test_predict-test_y[:len(test_predict)])/test_y[:len(test_predict)])  #偏差
        #以折线图表示结果
        plt.figure()
        plt.plot(list(range(len(test_predict))), test_predict, color='b')
        plt.plot(list(range(len(test_y))), test_y,  color='r')
        plt.show()

#prediction() 